import { Copy } from '@/utils/object.js'
import { Getsite } from '@/utils/amap.js'
// TODO 手动选择全市的时候，加载，  区域选择的时候更新数据；
// TODO 按范围选择的列表数据处理
export default async function(cb){
    // 超过时间还未加载完成的话，出现loading
    setTimeout(() => {
        if(this.loading === -1){
            this.loading = 0       
        }
    }, 1000)
    
    Getsite().then(e => {
        console.log(e)
        this.getCityCallback({ district: e[2] })
    })
    try {
        // 获取深圳城市区域列表
        const citysResponse = await HTTP('order/selectArea', {
            cityUid: 202
        })
        if(citysResponse.code !== 200){
            this.$toast('服务器异常')
            return
        }   
        let citys = citysResponse.data.map(e => ({...e, name: e.areaName}))
        // 修改全部区域
        this.cityList = this.cityList.map((e, index) => {
            if(index === 1){
                return {
                    ...e,
                    citys,
                }
            }
            return e
        })
        // 获取选中数据
        const responseData = await HTTP('serve_scope_list', {
            userUid: getuserUid(),
        })
        if(responseData.code !== 200){
            this.$toast('服务器异常')
            return
        }
        // 获取城市选中数据
        let selectedAreaList = responseData.data.filter((e) => e.areaOrRange == 1)
            .map(selestedcity => citys.find(city => city.areaUid === selestedcity.areaUid)
                ? citys.find(city => city.areaUid === selestedcity.areaUid).areaName
                : (console.log('后台异常， areaUid匹配不上'), this.$toast('后台异常，areaUid匹配不上'), false)
            )
            .filter(e => e)
        let serveList = responseData.data.filter((e) => e.areaOrRange == 2)
            .map(scopeSite => ({
                ...scopeSite,
                name: scopeSite.rangeName,
                rangeSize: scopeSite.rangeSize,
                position: {
                    lng: scopeSite.longitude,
                    lat: scopeSite.latitude
                }
            }))
        this.serveList = serveList
        // {
        //     name: this.nearestJunction,
        //     position: this.currentPosition,
        //     rangeSize: this.cp,
        // }
        // 选中的长度和所有的数据，那么就是选中全市
        if(selectedAreaList.length === citys.length){
            // 激活全市选中
            this.allCityChecked = true
            this.cityListSelected = Copy(selectedAreaList)
            this.dbCityListSelected = Copy(selectedAreaList)
        }else{ // 选中部分数据
            this.allCityChecked = false
            this.cityListSelected = Copy(selectedAreaList)
            this.dbCityListSelected = Copy(selectedAreaList)
        }
        console.log(this.cityListSelected)
        this.citys = citys
        this.responseCitySelectData = responseData.data
        console.log(citysResponse)
        console.log(responseData)
        console.log('---citys---')
        console.log(citys)
        this.loading = 1
        cb && cb()   
    } catch (error) {
        this.$toast('服务器异常')
    }
}